//
//  ConnectionManager.m
//  GravityControl
//
//  Created by Javier Quevedo on 28/06/2009.
//  Copyright 2009 VRIJE. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#import "ConnectionManager.h"
#import "GravityAgent.h"




@implementation ConnectionManager

	struct sockaddr_in si_other;

	static ConnectionManager *sharedConnectionManager = nil;

int connectToTcpServer(){
	int sock;//, bytes_recieved;  
	//char send_data[1024],recv_data[1024];
	struct hostent *host;
	struct sockaddr_in server_addr;  
	
	host = gethostbyname(IP);
	
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		perror("Socket");
		exit(1);
	}
	
	server_addr.sin_family = AF_INET;     
	server_addr.sin_port = htons(PORT);   
	server_addr.sin_addr = *((struct in_addr *)host->h_addr);
	bzero(&(server_addr.sin_zero),8); 
	
	if (connect(sock, (struct sockaddr *)&server_addr,
				sizeof(struct sockaddr)) == -1) 
	{
		perror("Connect");
		return NO;
	}
	//write(sock,"hola",strlen("hola")); 
	
	return sock;
	
	//close(sock);
	
}

int createUdpSocket(){
	/* Create the UDP socket */

	int s;	
	if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
		printf("Problem creating the socket");
    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
	if (inet_aton(IP, &si_other.sin_addr)==0) 
	      printf("Error binding\n");
	
	return s;
}



- (void) send: (NSString *)message{
	char * aPointer = (char *) [message cStringUsingEncoding:NSASCIIStringEncoding];
	char angleCString[strlen(aPointer)];
	strncpy(angleCString, aPointer, strlen(aPointer));
	
	if (udpSocket)
		sendto(udpSocket, angleCString, sizeof(angleCString), 0,(struct sockaddr *) &si_other, sizeof(si_other));
	//write(udpSocket, angleCString, sizeof(angleCString));
}
	

- (void) connect{
	udpSocket = createUdpSocket();
}


+ (ConnectionManager*)sharedManager
{
    @synchronized(self) {
        if (sharedConnectionManager == nil) {
            [[self alloc] init]; // assignment not done here
			
			
        }
    }
    return sharedConnectionManager;
}

+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (sharedConnectionManager == nil) {
            sharedConnectionManager = [super allocWithZone:zone];
			[sharedConnectionManager connect];
			
			
            return sharedConnectionManager;  // assignment and return on first allocation
        }
    }
    return nil; //on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (unsigned)retainCount
{
    return UINT_MAX;  //denotes an object that cannot be released
}


- (void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}



/*
-(id) init{
	if (self = [super init])
	{
		gravityAgent = [GravityAgent sharedAgent];
		udpSocket = createUdpSocket();
		[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(trackNotificationAccelerometer:) 
												 name:nGravityUpdated object:gravityAgent];
	}
		return self;
}
*/

@end
